package com.briup.jz.web.controller;

import com.briup.jz.bean.Role;
import com.briup.jz.bean.RolePrivilege;
import com.briup.jz.bean.User;
import com.briup.jz.bean.extend.RolePrivilegeExtend;
import com.briup.jz.bean.extend.UserExtend;
import com.briup.jz.service.IUserService;
import com.briup.jz.utils.Message;
import com.briup.jz.utils.MessageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: jz
 * @description: 用户控制器
 * @author: catmilko
 * @create: 2020-06-11 15:59
 **/

@RestController
@RequestMapping("user")
@Api(description = "用户管理接口")
public class UserController {
    @Autowired
    private IUserService userService;

    @ApiOperation(value = "查询所有用户权限")
    @GetMapping("findAll")
    public Message findAll() {
        List<User> list = userService.findAll();
        return MessageUtil.success(list);
    }

    /**
     * 查询所有员工
     * @return
     */
    @ApiOperation(value = "审核员工")
    @GetMapping("findAllEmployee")
    public Message findAllEmployee(){ return MessageUtil.success(userService.findAllEmployee());
    }
    @GetMapping("auditing")
    public Message auditing(long id){
        User user = userService.findById(id);
        user.setStatus("已通过");
        userService.saveOrUpdate(user);
        return MessageUtil.success("审核成功");
    }
    @GetMapping("auditing1")
    public Message auditing1(long id){
        userService.deleteById(id);
        return MessageUtil.success("审核成功");
    }
    @GetMapping("refuseauditing1")
    public Message refuseauditing1(long id){
        userService.deleteById(id);
        return MessageUtil.success("已拒绝审核");}

    @GetMapping("refuseauditing")
    public Message refuseauditing(long id){
        User user =  userService.findById(id);
        user.setStatus("未通过");
        userService.saveOrUpdate(user);
        return MessageUtil.success("已拒绝审核");}



    @ApiOperation(value = "通过id查询用户权限")
    @ApiImplicitParam(name = "id", value = "类别唯一编号", required = true, paramType = "query")
    @GetMapping("findById")
    public Message findById(long id) {
        User user = userService.findById(id);
        return MessageUtil.success(user);
    }

    @ApiOperation(value = "保存或通过id更新用户权限")
    @PostMapping("saveOrUpdate")
    public Message saveOrUpdate(User user) {
       userService.saveOrUpdate(user);
        return MessageUtil.success("保存成功");
    }

    @ApiOperation(value = "通过id删除用户权限")
    @ApiImplicitParam(name = "id", value = "类别唯一编号", required = true, paramType = "query")
    @GetMapping("deleteById")
    public Message deleteById(long id) {
        userService.deleteById(id);
        return MessageUtil.success("删除成功");
    }


    @ApiOperation(value = "查询所有用户及其角色信息")
    @GetMapping("findAllWithRole")
    public Message findAllWithRole(){
        List<UserExtend> list = userService.findAllWithRole();
        return MessageUtil.success(list);
    }
}
